package rabbitmq.simply;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProductorDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 创建对应的工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("8.152.203.113");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("bite");
        // 2. 创建connection
        Connection connection = connectionFactory.newConnection();
        // 3. 创建 channel
        Channel channel = connection.createChannel();
        // 4. 设置queue 和 exchange

        /**
         * queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *                                  Map<String, Object> arguments)
         * queue：队列名称
         * durable：是否持久化
         * exclusive：是否独占
         * autoDelete：是否自动销毁
         * arguments：参数
         */
        channel.queueDeclare("hello", true, false, true, null);

        /**
         * 发送消息
         * basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
         * exchange：交换机名称
         * routingKey：queue的对象
         * props：一些属性
         * body：传输消息
         */

        for (int i = 0; i < 10; i++) {
            String msg = "hello rabbitmq 0.0";
            channel.basicPublish("", "hello", null, msg.getBytes());
        }

        System.out.println("传输完成");
        channel.close();
        connection.close();

    }
}
